module.exports = function ($state, $resource, toaster) {

	return {

		restrict: 'E',
		template: require('../views/productEntity.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'util': '='
		},
		link: function (scope, elements, attrs) {
			scope.saleinfo = {
				'sale_code': scope.saleobj.code,
				'entity_confirm_receipt': '7',
			};
			scope.formdata = {
				'entity_label': [
					{'id':'1', 'label': '正品保证','check': false},
					{'id':'2', 'label': '7天无理由退换','check': false},
					{'id':'3', 'label': '保修','check': false}
				], //系统定义标签
				'arealist1': [], //发货地省份
				'arealist2': [], //发货地城市
				'label_custom': '',
				'entity_label_custom': [], //自定义标签列表
				'delivery_type_list': [
					{'id': '1', 'label': '邮寄到户'},
					{'id': '2', 'label': '配送站点'},
				],
				'confirm_num': [	//默认确认收货时间
					{'id': 1, 'label': '1'},
					{'id': 2, 'label': '2'},
					{'id': 3, 'label': '3'},
					{'id': 4, 'label': '4'},
					{'id': 5, 'label': '5'},
					{'id': 6, 'label': '6'},
					{'id': 7, 'label': '7'},
					{'id': 8, 'label': '8'},
					{'id': 9, 'label': '9'},
					{'id': 10, 'label': '10'},
					{'id': 11, 'label': '11'},
					{'id': 12, 'label': '12'},
					{'id': 13, 'label': '13'},
					{'id': 14, 'label': '14'},
					{'id': 15, 'label': '15'},
				]
			};

			scope.save = function(){
				if(!scope.saleinfo.entity_supplier){
					toaster.warning({title: '', body: '未填写产品供应商'});
					return;
				}
				// if(!scope.saleinfo.entity_category){
				// 	toaster.warning({title: '', body: '未选择实物分类'});
				// 	return;
				// }
				if(!scope.formdata.area1 || !scope.formdata.area2){
					toaster.warning({title: '', body: '未选择发货城市'});
					return;
				}
				scope.saleinfo.entity_goods_location = scope.formdata.area1.name + '-' + scope.formdata.area2.name;
				scope.saleinfo.entity_label = '';
				scope.formdata.entity_label.forEach(element => {
					if(element.check){
						scope.saleinfo.entity_label += ',' + element.label;
					}
				});
				scope.saleinfo.entity_label_custom = scope.formdata.entity_label_custom.toString();
				scope.saleinfo.entity_label = scope.saleinfo.entity_label.substring(1);

				$resource('/api/as/tc/ticketsalesku/saveSaleEntityInfo', {}, {}).save(scope.saleinfo, function(res){
					if (res.errcode != 0) {
						toaster.error({ title: '', body: '保存失败'});
						return;
					}
					toaster.success({title: '', body: '成功'});
				});
			}

			//查询省份code=''和城市code=省编号
			scope.getAreaList = function(code, name){
				$resource('/api/us/sc/city/arealist', {}, {}).save({'code':code}, function(res){
				  if (res.errcode != 0) {
					  return;
				  }
				  scope.formdata[name] = res.data;
			  });
			}

			//初始化获取实物信息
			function getEntityInfo(){
				$resource('/api/as/tc/ticketsalesku/getSaleEntityInfo', {}, {})
				  .save({'sale_code':scope.saleobj.code}, function(res){
					if (res.errcode != 0) {
						return;
					}
					scope.saleinfo = res.data;
					scope.formdata.entity_label_custom = res.data.entity_label_custom.split(',');
					scope.formdata.entity_label.forEach(element => {
						if(res.data.entity_label.indexOf(element.label) >= 0){
							element.check = true;
						}
					});
					var index = res.data.entity_goods_location.indexOf('-');
					scope.formdata.area1 = {'code': '', 'name': res.data.entity_goods_location.substring(0,index)}
					scope.formdata.area2 = {'code': '', 'name': res.data.entity_goods_location.substring(index+1)}
				});
				scope.getAreaList('', 'arealist1');
			}
			getEntityInfo();

			//添加自定义标签
			scope.addLabelCustom = function(){
				if(!scope.formdata.label_custom) return;
				for(var i = 0; i < scope.formdata.entity_label_custom.length; i++){
					var temp = scope.formdata.entity_label_custom[i];
					if(temp == scope.formdata.label_custom){
						toaster.warning({ title: '', body: '无法重复添加标签'});
						return;
					}
				}
				scope.formdata.entity_label_custom.push(scope.formdata.label_custom);
				scope.formdata.label_custom = ''
			}

			scope.removeLabel = function(index){
				console.log(index);
				scope.formdata.entity_label_custom.splice(index,1);
			}
			
		}
	};

};


